package de.bazinga.rotw.collision;

import java.util.Iterator;

import com.jmex.game.state.GameStateManager;
import com.jmex.jbullet.collision.CollisionEvent;
import com.jmex.jbullet.collision.CollisionListener;
import com.jmex.jbullet.collision.CollisionObject;

import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.network.BasicSyncManager;
import de.bazinga.rotw.network.Client;
import de.bazinga.rotw.network.Server;
import de.bazinga.rotw.network.message.HitMessage;
import de.bazinga.rotw.network.message.NewBulletMessage;
import de.bazinga.rotw.person.OpponentList;
import de.bazinga.rotw.person.Person;
import de.bazinga.rotw.player.Player;
import de.bazinga.rotw.vehicle.Car;
import de.bazinga.rotw.vehicle.VehicleHitMessage;

public class GameCollisionListener implements CollisionListener {
	
	
    public void collision(CollisionEvent event){
    	Player player = ((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getPlayer();
    	
        //get collision force
        if(player != null && player.hasVehicle() && 
        		player.getVehicle() instanceof Car &&
    			((Car)player.getVehicle()) != null &&
        		((Car)player.getVehicle()).getPhysicsCar() != null &&
        		event.getAppliedImpulse() > 200) {
        	
        	if(event.getNodeA() != null && 
        			((Car)player.getVehicle()).getPhysicsCar() != null &&
        			event.getNodeA() == ((Car)player.getVehicle()).getPhysicsCar()) {
        		vehicleCollision(event, event.getNodeB());
        		//System.out.println(event.getAppliedImpulse());
        	}
        	if(event.getNodeB() != null && 
        			((Car)player.getVehicle()) != null &&
        			((Car)player.getVehicle()).getPhysicsCar() != null &&
        			event.getNodeB() == ((Car)player.getVehicle()).getPhysicsCar()) {
        		vehicleCollision(event, event.getNodeA());
        		//System.out.println(event.getAppliedImpulse());
        	}
        }
        
        
    }
    
    
    private void vehicleCollision(CollisionEvent event, CollisionObject otherNode) {
    	Player player = ((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getPlayer();
    	((Car)player.getVehicle()).setDamage(player.getVehicle().getDamage() + 10, true);
    	
    	// Check for Person-Collision
    	Iterator<Person> it = OpponentList.getInstance().getOpponents().values().iterator();
    	while(it.hasNext()) {
    		Person p = it.next();
    		if(p.getBodyPhysicsNode() == otherNode) {
    			BasicSyncManager syncManager = ((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getSyncManager();
                
                if(syncManager instanceof Server) {
                        // Kill Person
                       	p.hit(player, 100);
                        
                        HitMessage hitMessage = new HitMessage();
                        hitMessage.setPlayerName(player.getPlayerName());
                        hitMessage.setPlayerLocation(player.getCameraPosition());
                        hitMessage.setDamage(100);
                        hitMessage.setWeaponFlag(NewBulletMessage.VEHICLE);
                        
                        ((Server) syncManager).getServer().sendToPlayer(hitMessage, p.getPlayerId());
                }else if(syncManager instanceof Client) {
                	VehicleHitMessage vhm = new VehicleHitMessage();
                	vhm.setPersonHitIndex(p.getPlayerId());
                    ((Client) syncManager).getClient().sendToServer(vhm);
                }
    		}
    	}
    }
}